/**    
 * 文件名：SendByRoomService.java</br>
 *    
 * 版本信息：v1.0</br>
 * 日期：2018年2月8日</br>
 * © 2005-2017 雷技信息科技（上海）有限公司版权所有</br>
 *
 */
package com.logic.landseaserver.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.logic.landseaserver.common.LandeaConstants.DdingConstant;
import com.logic.landseaserver.common.LandseaErrorMessageEnum;
import com.logic.landseaserver.common.exception.LandseaException;
import com.logic.landseaserver.common.exception.ResultCodes.SendPwdResultCode;
import com.logic.landseaserver.domain.DdingRoomSync;
import com.logic.landseaserver.domain.Room;
import com.logic.landseaserver.persistence.read.DdingRoomSyncReadMapper;
import com.logic.landseaserver.persistence.read.RoomReadMapper;
import com.logic.landseaserver.service.IDdLockService;
import com.logic.landseaserver.ws.request.SendAddPwdByRoomReq;

/**
 *
 * 项目名称：system-server</br>
 * 类名称：SendByRoomService</br>
 * 类描述：测试用</br>
 * 创建人：Aaron</br>
 * 创建时间：2018年2月8日 下午3:16:53</br>
 * 
 * @version 1.0
 *
 */
@Service
public class SendByRoomService
{
    private static final Logger LOG = LoggerFactory.getLogger(SendByRoomService.class);
    
    @Autowired
    private RoomReadMapper roomReadMapper;
    
    @Autowired
    private DdingRoomSyncReadMapper ddingRoomSyncReadMapper;
    
    @Autowired
    private IDdLockService ddLockService;
    
    public List<String> sendAddPwds(SendAddPwdByRoomReq req)
        throws LandseaException
    {
        String rooms = req.getRooms();
        if (StringUtils.isEmpty(rooms))
        {
            throw LandseaException.createException(SendPwdResultCode.E010510001);
        }
        
        Boolean isSend = req.isSendSms();
        if (null == isSend)
        {
            throw LandseaException.createException(SendPwdResultCode.E010510002);
        }
        
        List<String> errorList = new ArrayList<String>();
        
        String[] roomsArr = rooms.split(",");
        for (String roomId : roomsArr)
        {
            try
            {
                Integer id = Integer.parseInt(roomId);
                sendAddPwd(id, isSend.booleanValue());
            }
            catch (Exception e)
            {
                LOG.error("sendAddPwds发送roomId=" + roomId + "密码失败.", e);
                errorList.add(roomId);
            }
        }
        
        return errorList;
    }
    
    /**
     * [描述]：重置密码的有效期</br>
     *
     * @param roomId 房间Id
     * @param contractId 合同id
     * @param pwdEndTime 门锁失效时间（禁止设置为过去的时间，不然会失败）,如果为null，则根据账单实际情况走
     * @throws LandseaException
     */
    public void sendAddPwd(Integer roomId, boolean isSendSms) throws LandseaException
    {
        LOG.info("sendPwd|发送密码房间ID=" + roomId);
        
        // 检查房间是否存在
        Room room = roomReadMapper.selectRoomInfo(roomId);
        
        if (null == room)
        {
            throw LandseaException.createException(LandseaErrorMessageEnum.DDINGLOCK_ROOM_NOT_EXISTS_ERROR);
        }
        
        // 查询同步记录
        DdingRoomSync ddingRoomSync = ddingRoomSyncReadMapper.selectByPrimaryKey(roomId);
        
        if (null == ddingRoomSync || DdingConstant.SYNCSTATE_YES != ddingRoomSync.getSyncState())
        {
            // 没有同步过，不能修改密码
            throw LandseaException.createException(LandseaErrorMessageEnum.DDINGLOCK_NONESYNC_CANNOT_MODIFYPWD_ERROR);
        }
        
        if (DdingConstant.SYNCSTATE_YES != ddingRoomSync.getLockState())
        {
            // 没有安装门锁，不能修改密码
            throw LandseaException.createException(LandseaErrorMessageEnum.DDINGLOCK_NONE_LOCK_ERROR);
        }
        
        // 新增，随机生成密码，按照合同房租来
        ddLockService.addLockPassword(ddingRoomSync, true, null, isSendSms);
    }
}
